package com.hgc.ch01.sync;

/**
 * Created by 01367627 on 2019/6/14.
 */
public class SyncMethod {

    /**
     * volatile 轻量级同步，并不能代替synchronized实现锁效果
     */
    private static volatile int i;

    synchronized static void add(){
        i++;
    }

    static class MyThread extends Thread{
        @Override
        public void run() {
            for(int j=0;j<10000;j++){
                add();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new MyThread();
        Thread t2 = new MyThread();
        t1.start();
        t2.start();
        Thread.sleep(100);
        System.out.println(i);
    }
}
